//
//  MeRegisterSucceedVC.m
//  ZITHER
//
//  Created by weixin on 2019/1/16.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "MeRegisterSucceedVC.h"
#import "MeSetUsetImgVC.h"
#import "MeSetUserName.h"

#import "MyQRCodeWebVC.h"
#import "TaskToastView.h"
#import "CRBBoxHomeHttp.h"

@interface MeRegisterSucceedVC ()

@property (nonatomic,strong)BaseImageV * img;
@property (nonatomic,strong)BaseLab * succeedLab;
@property (nonatomic,strong)BaseLab * succeedTipeLab;
@property (nonatomic,strong)BaseView * garyV;
@property (nonatomic,strong)BaseImageV * userPic;
@property (nonatomic,strong)BaseLab * userName;
@property (nonatomic,strong)BaseLab * userPhone;
@property (nonatomic,strong)BaseBtn * donebtn;

@property (nonatomic,strong)TaskToastView *taskToasView;
@property (nonatomic,strong)UIView *maskView;

@end

@implementation MeRegisterSucceedVC

- (void)viewDidLoad {
    [super viewDidLoad];
   // [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showTaskToast:) name:@"kPUSH_TASL_REGIST_FINISHED" object:nil];
    [self setNav];
    [self setUI];
    [self setMasonry];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"0"]) {
        [self showTaskToast:kUSER_INFO_MANAGER.userId];
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setData];
}

-(void)setData{
    self.userName.text = kUSER_INFO_MANAGER.nickName;
    [self.userPic sd_setImage:kUSER_INFO_MANAGER.avatar];
    if ( kUSER_INFO_MANAGER.phone.length>0) {
        self.userPhone.text = kUSER_INFO_MANAGER.phone;
    }else{
        self.userPhone.text = kUSER_INFO_MANAGER.email;
    }
}

-(void)setNav{
    // Do any additional setup after loading the view.
    /** 还是会出现手动侧滑返回会有系统导航栏遮盖的情况 需要注意下 */
    [self.baseNavigationView setHiddenNav:YES VC:self];
    [self setNavigationTitle:@""];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    [self.baseNavigationView setBackgroundColor:[UIColor clearColor]];
    self.baseNavigationView.bottomLineView.backgroundColor = [UIColor clearColor];
    self.baseNavigationView.isShowShadow = YES;
    //    登录或注册
    self.baseNavigationView.leftButton1.hidden = YES;
    [self.baseNavigationView setBackgroundColor:[UIColor clearColor]];
    self.baseNavigationView.bottomLineView.backgroundColor = [UIColor clearColor];
}

-(void)setMasonry{

    [self.garyV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view).offset(0);
        make.height.mas_equalTo(200);
    }];
    
    [self.img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(55);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.width.mas_equalTo(50);
    }];
    [self.succeedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.img.mas_bottom).offset(20);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.mas_equalTo(20);
    }];
    [self.succeedTipeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.succeedLab.mas_bottom).offset(5);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.mas_equalTo(17);
    }];
    [self.userPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.garyV.mas_bottom).offset(50);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.width.mas_equalTo(120);
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPic.mas_bottom).offset(15);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.mas_equalTo(20);
    }];
    [self.userPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userName.mas_bottom).offset(5);
        make.centerX.equalTo(self.view.mas_centerX).offset(0);
        make.height.mas_equalTo(13);
    }];
    [self.donebtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
        make.height.mas_equalTo(45);
        make.top.equalTo(self.userPhone.mas_bottom).offset(50);
    }];
}


-(void)setUI{
    [self.view addSubview:self.garyV];
    [self.view addSubview:self.img];
    [self.view addSubview:self.succeedLab];
    [self.view addSubview:self.succeedTipeLab];
    [self.view addSubview:self.userPic];
    [self.view addSubview:self.userName];
    [self.view addSubview:self.userPhone];
    [self.view addSubview:self.donebtn];

}

-(BaseImageV *)img{
    if (!_img) {
        _img = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        _img.image = [UIImage imageNamed:@"注册成功_icon"];
        _img.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    }
    return _img;
}

-(BaseLab *)succeedLab{
    if (!_succeedLab) {
        _succeedLab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 0) text:@"注册成功" font:10 textColor:HexColor(0x333333) isBold:NO numberOfLines:1];
        _succeedLab.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:15];

    }
    return _succeedLab;
}
-(BaseLab *)succeedTipeLab{
    if (!_succeedTipeLab) {
        _succeedTipeLab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 0) text:@"你可以设置自己喜欢的用户名和头像" font:10 textColor:HexColor(0x999999) isBold:NO numberOfLines:1];
        _succeedTipeLab.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];

    }
    return _succeedTipeLab;
}

-(BaseView *)garyV{
    if (!_garyV) {
        _garyV = [[BaseView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _garyV.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
    }
    return _garyV;
}

- (BaseImageV *)userPic{
    if (!_userPic) {
        _userPic = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        _userPic.image = [UIImage imageNamed:@"注册成功_默认头像"];
        _userPic.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
        [_userPic setCornerRadiusWithRadius:60 isMasks:YES];
        @weakify(self);
        [[_userPic addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            MeSetUsetImgVC *vc = [[MeSetUsetImgVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _userPic;
}

-(BaseLab *)userName{
    if (!_userName) {
        _userName = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 0) text:@"设置用户名称" font:10 textColor:HexColor(0x333333) isBold:NO numberOfLines:1];
        _userName.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        [[_userName addTag]subscribeNext:^(id  _Nullable x) {
            MeSetUserName *vc = [[MeSetUserName alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }];
    }
    return _userName;
}
- (BaseLab *)userPhone{
    if (!_userPhone) {
        _userPhone = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 0) text:@"" font:10 textColor:HexColor(0x333333) isBold:NO numberOfLines:1];
        _userPhone.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
    }
    return _userPhone;
}


- (BaseBtn *)donebtn{
        if (!_donebtn) {
            _donebtn = [[BaseBtn alloc]initializeFrame:CGRectMake(0, 0, 0, 0) NormalText:@"完成" NormalTextColor:[UIColor whiteColor] SeleText:nil SeleTextColor:nil font:14 isBold:NO];
            _donebtn.backgroundColor = baseColorTheme;
            _donebtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];
            [_donebtn setCornerRadiusWithRadius:4 isMasks:YES];
            /** 按钮点击案例 */
            @weakify(self);
            [[_donebtn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self);
                [self.navigationController popToRootViewControllerAnimated:YES];
            }];
        }return _donebtn;
}

#pragma mark - taskToast
- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [UIView new];
        _maskView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    }
    return _maskView;
}
- (TaskToastView *)taskToasView {
    __weak __typeof(self)weakSelf = self;
    if (!_taskToasView) {
        _taskToasView = [[NSBundle mainBundle]loadNibNamed:@"TaskToastView" owner:nil options:nil][0];
        _taskToasView.pushWebBlock = ^{
            MyQRCodeWebVC * vc = [[MyQRCodeWebVC alloc]init];
            vc.navStyle = 1;
            [weakSelf.navigationController pushViewController:vc animated:YES];
            vc.url = [NSString stringWithFormat:@"%@/#/activity/merchant",RootUrlWeb];
        };
        _taskToasView.cancelBlock = ^{
            if (weakSelf.taskToasView) {
                [weakSelf.taskToasView removeFromSuperview];
                weakSelf.taskToasView = nil;
            }
            if (weakSelf.maskView) {
                [weakSelf.maskView removeFromSuperview];
                weakSelf.maskView = nil;
            }
        };
    }
    return _taskToasView;
}

- (void)showTaskToast:(NSString *)userId {
    if (!_taskToasView) {
        [CRBBoxHomeHttp requestTaskAfterRegistWithUserId:userId success:^(NSURLSessionDataTask *task, id responseObject) {
            
            //如果金额为零不显示
            TaskModel *model = responseObject;
            if ([model.accumulateMoney intValue] > 0.001) {
            }else{
                return ;
            }
            
            [self.view addSubview:self.maskView];
            [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(self.view);
            }];
            [self.maskView addSubview:self.taskToasView];
            [self.taskToasView setData:responseObject];
            [self.taskToasView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.size.mas_equalTo(CGSizeMake(258, 416));
                make.centerX.equalTo(self.maskView);
                make.centerY.equalTo(self.maskView);
            }];
        } fail:^(NSURLSessionDataTask *task, NSError *error, NSString *code, NSString *msg) {
            [Tools showTips:msg];
        }];
        
    }
}

@end
